package com.example.imtoken.modules.main.wallet.create

import android.app.Dialog
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.ScreenUtils
import com.example.imtoken.R
import com.example.imtoken.databinding.CreateWalletDialogFragmentBinding
import com.example.imtoken.modules.main.wallet.manage.wallet.imp.key.KeyStoreImportWalletActivity
import com.example.imtoken.modules.main.wallet.manage.wallet.imp.mnemonic.MnemonicImportWalletActivity
import com.example.imtoken.modules.main.wallet.manage.wallet.imp.privates.PrivateImportWalletActivity
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment

class CreateWalletDialogFragment(val coin: String) : BottomSheetDialogFragment() {

    private val viewModel: CreateWalletDialogViewModel by lazy {
        ViewModelProvider(this).get(CreateWalletDialogViewModel::class.java)
    }

    private lateinit var dataBinding: CreateWalletDialogFragmentBinding

    private var mBehavior: BottomSheetBehavior<View>? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //重写样式,设置dialog背景色为透明
        setStyle(STYLE_NORMAL, R.style.AppBottomSheet)
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
        dataBinding =
            DataBindingUtil.inflate(
                layoutInflater,
                R.layout.create_wallet_dialog_fragment,
                null,
                false
            )
        dialog.setContentView(dataBinding.root)
        val lp = dataBinding.root.layoutParams
        lp.height = ScreenUtils.getAppScreenHeight() - ConvertUtils.sp2px(50f)
        dataBinding.root.layoutParams = lp
        mBehavior = BottomSheetBehavior.from(dataBinding.root.parent as View)
        initViewContent()
        dataBinding.viewModel = viewModel
        return dialog
    }

    private fun initViewContent() {
        dataBinding.tvWalletName.text = coin.toUpperCase()
        dataBinding.ivBlack.setOnClickListener { dismiss() }
        dataBinding.llGotoCreateWallet.setOnClickListener {
            val intent = Intent(activity, CreateWalletActivity::class.java)
            intent.putExtra("coin", coin)
            ActivityUtils.startActivity(intent)
            dismiss()
        }
        dataBinding.llMnemonicImp.setOnClickListener {
            //助记词导入
            val intent = Intent(activity, MnemonicImportWalletActivity::class.java)
            intent.putExtra("coin", coin)
            ActivityUtils.startActivity(intent)
            dismiss()
        }
        dataBinding.llPrivateImp.setOnClickListener {
            //私钥导入
            val intent = Intent(activity, PrivateImportWalletActivity::class.java)
            intent.putExtra("coin", coin)
            ActivityUtils.startActivity(intent)
            dismiss()
        }
        dataBinding.llKeyStoreImp.setOnClickListener {
            //keyStore导入
            val intent = Intent(activity, KeyStoreImportWalletActivity::class.java)
            intent.putExtra("coin", coin)
            ActivityUtils.startActivity(intent)
            dismiss()
        }
    }

    override fun onStart() {
        super.onStart()
        mBehavior?.state = BottomSheetBehavior.STATE_EXPANDED
    }
}